﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Data.Seedwork;
using Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Domain.MainBoundedContext.ReportModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using NHibernate;
using Huirui.Cavan.Core.Extension;
using Spring.Data.Common;
using System.Data;
using System.Collections;

namespace Huirui.Cavan.Infrastructure.Data.MainBoundedContext.PurchaseModule
{
    public class PayRepository : Repository<Pay, PaySearch>, IPayRepository
    {
        public IList<UnPayAndPayReport> GetUnPayAndPayReport(DateTime? startDate, DateTime? endDate, string supplierCode, int maxRowsPerPage, int currentPage)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("SELECT p.SupplierCode, s.Name as SupplierName, p.Code as UnPayOrPayCode, p.CreateDate as UnPayOrPayDate, p.Currency, p.CurrencyRate, 0 as FinalAmount, p.Amount, -p.Amount AS LeftAmount, 0 as BusinessType ");
            sql.Append("FROM Pay p INNER JOIN Supplier s WITH (NOLOCK) ON  p.SupplierCode = s.Code ");
            sql.Append("WHERE 1 = 1 ");
            if(startDate.HasValue && endDate.HasValue)
            {
                sql.Append("AND  p.CreateDate >= @StartDate AND p.CreateDate <= @EndDate ");
            }
            if(supplierCode.IsNotEmpty())
            {
                sql.Append("AND s.Code = @SupplierCode ");
            }
            sql.Append("UNION ALL ");
            sql.Append("SELECT t.SupplierCode, t.Name as SupplierName, up.UnPayCode as UnPayOrPayCode, up.CreateDate as UnPayOrPayDate, up.Currency, up.CurrencyRate, up.FinalAmount, up.LeftAmount, 0 as Amount, up.UnPayType AS BusinessType ");
            sql.Append("FROM UnPay up INNER JOIN PayDetail pd WITH (NOLOCK) ON  pd.UnPayID = up.ID INNER JOIN ( ");
            sql.Append("SELECT p.SupplierCode, s.Name, p.Code, p.CreateDate, p.Currency, p.CurrencyRate, p.Amount,-p.Amount AS LeftAmount ");
            sql.Append("FROM Pay p INNER JOIN Supplier s WITH (NOLOCK) ON  p.SupplierCode = s.Code ");
            sql.Append("WHERE  1 = 1 ");
            if(startDate.HasValue && endDate.HasValue)
            {
                sql.Append("AND  p.CreateDate >= @StartDate AND p.CreateDate <= @EndDate ");
            }
            if(supplierCode.IsNotEmpty())
            {
                sql.Append("AND s.Code = @SupplierCode ");
            }
            sql.Append(") AS t ON  pup.PayCode = t.Code ");

            IDbParameters paras = this.AdoTemplate.CreateDbParameters();
           
            if (startDate.HasValue && endDate.HasValue)
            {
                paras.Add("StartDate", DbType.DateTime).Value = startDate.Value;
                paras.Add("EndDate", DbType.DateTime).Value = endDate.Value;
            }
            if (supplierCode.IsNotEmpty())
            {
                paras.Add("SupplierCode", DbType.String).Value = supplierCode;
            }

            return this.AdoTemplate.QueryWithRowMapper<UnPayAndPayReport>(CommandType.Text, sql.ToString(), new UnPayAndPayReportMapper(), paras);
        }

        public IList<PayReport> GetPayReport(string payCode, DateTime? startDate, DateTime? endDate, string supplierCode)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("SELECT p.Code AS PayCode, p.PayDate, p.SupplierCode, s.Name AS SupplierName, ");
            sql.Append("p.Currency, p.CurrencyRate, p.Amount, p.AccountCode, p.AccountNumber, p.Remark ");
            sql.Append("FROM Pay p WITH (NOLOCK) ");
            sql.Append("INNER JOIN Supplier s WITH(NOLOCK) ON s.Code = p.SupplierCode ");
            sql.Append("INNER JOIN UserAccount ua WITH (NOLOCK) ON p.EditUser = ua.ID ");
            sql.Append("WHERE  1 = 1 ");
            if (startDate.HasValue && endDate.HasValue)
            {
                sql.Append("AND  p.PayDate >= @StartDate AND p.PayDate <= @EndDate ");
            }
            if (supplierCode.IsNotEmpty())
            {
                sql.Append("AND p.SupplierCode = @SupplierCode ");
            }
            if (payCode.IsNotEmpty())
            {
                sql.Append("AND p.Code = @PayCode ");
            }

            IDbParameters paras = this.AdoTemplate.CreateDbParameters();

            if (startDate.HasValue && endDate.HasValue)
            {
                paras.Add("StartDate", DbType.DateTime).Value = startDate.Value;
                paras.Add("EndDate", DbType.DateTime).Value = endDate.Value;
            }
            if (supplierCode.IsNotEmpty())
            {
                paras.Add("SupplierCode", DbType.String).Value = supplierCode;
            }
            if (payCode.IsNotEmpty())
            {
                paras.Add("PayCode", DbType.String).Value =payCode;
            }

            return this.AdoTemplate.QueryWithRowMapper<PayReport>(CommandType.Text, sql.ToString(), new PayReportMapper(), paras);
        }
    }
}
